package com.example.huainanlive.ui

import android.app.Activity
import android.content.Intent
import android.view.View
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.demon.androidbasic.base.MyActivity
import com.example.huainanlive.R
import com.example.huainanlive.adapter.LiveHousePreviewAdapter
import com.example.huainanlive.bean.LiveIdBean
import com.example.huainanlive.databinding.ActivityLiveHousePreviewBinding

class LiveHousePreviewActivity : MyActivity<ActivityLiveHousePreviewBinding>() {

    private lateinit var huiGuLiveViewModel: HuiGuLiveViewModel
    private lateinit var liveHousePreviewAdapter: LiveHousePreviewAdapter
    //页面跳转
    companion object {
        fun forward(
            activity: Activity,
        ) {
            val intent = Intent(activity, LiveHousePreviewActivity::class.java)
            activity.startActivity(intent)
        }
    }

    override fun initBind() {
        binding = ActivityLiveHousePreviewBinding.inflate(inflater)
    }

    override fun initData() {
        super.initData()

    }

    override fun initView() {

        liveHousePreviewAdapter= LiveHousePreviewAdapter()
        binding.rvLiveHousePreview.layoutManager = LinearLayoutManager(activity)
        binding.rvLiveHousePreview.adapter = liveHousePreviewAdapter
        binding.llSub.visibility = View.GONE
        huiGuLiveViewModel = ViewModelProvider(
            this)[HuiGuLiveViewModel::class.java]

        //直播预告列表
        huiGuLiveViewModel.getLiveHousePreview()
        huiGuLiveViewModel.getLiveHousePreviewBean.observe(this){LiveHousePreviewBean->
            LiveHousePreviewBean.forEach{LiveHousePreviewBeanItem->
                if (LiveHousePreviewBeanItem.sub== true){
                    binding.llSub.visibility = View.VISIBLE
                }
            }
            liveHousePreviewAdapter.setList(LiveHousePreviewBean)
        }

        liveHousePreviewAdapter.setOnItemClickListener { _, _, position ->
            LivePlayActivity.forward(this,liveHousePreviewAdapter.getItem(position).liveId,
                liveState = "直播预告")
        }

        liveHousePreviewAdapter.setOnItemChildClickListener { adapter, view, position ->

            val item = liveHousePreviewAdapter.getItem(position)
            when (view.id) {
                R.id.addSub -> {
                    if (item.sub==true){
                        huiGuLiveViewModel.cancelSub(LiveIdBean().apply {
                            liveId = item.liveId
                        })
                    }else{
                        huiGuLiveViewModel.addSub(LiveIdBean().apply {
                            liveId = item.liveId
                        })
                    }

                }
            }

        }




    }

}